/**
 * 
 */
package com.ginger.rabbitmq.convert;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.amqp.support.converter.MessageConverter;

/**
 * @Description: 文本消息类型转换
 * @author 姜锋
 * @date 2019年3月31日 下午4:41:24 
 * @version V1.0   
 *
 */
public class TextMessageConvert implements MessageConverter {
	
	@Override
	public Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionException {
		
		
		return new Message(object.toString().getBytes(),messageProperties);
	}
	
	@Override
	public Object fromMessage(Message message) throws MessageConversionException {
		String contentType  = message.getMessageProperties().getContentType();
		
		if(null != contentType && contentType.contains("text")) {
			return new String(message.getBody());
		}else {
		return message.getBody();
		}
	}
}
